﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using Alert_Form.Component.Alert;
using YXChatFormClient.Dtos;
using YXChatFormClient.Dtos.Account;
using YXChatFormClient.Dtos.Buddy;
using YXChatFormClient.Global;
using YXChatFormClient.Utility;

namespace YXChatFormClient.UserControls
{
    public partial class UC_SearchBuddy : UserControl
    {
        private readonly HttpHelper _http;
        public UC_SearchBuddy()
        {
            _http = new HttpHelper();
            InitializeComponent();
            //设置控件样式
            SetStyle(
                ControlStyles.AllPaintingInWmPaint | //不闪烁
                ControlStyles.OptimizedDoubleBuffer //支持双缓存
                , true);
            this.panelInfo.Visible = false;
        }

        private void UC_SearchBuddy_Load(object sender, EventArgs e)
        {
            tbSearch.Text = "请输入对方的YX号码";
        }

        private async void btnSearch_Click(object sender, EventArgs e)
        {
            string buddyNumber = tbSearch.Text;
            if (buddyNumber.IsNullOrWhiteSpace())
            {
                AlertForm.ShowAlert(@"查询的账号不能为空",AlertType.Warning);
                return;
            }
            Regex reg = new Regex(@"^[0-9]\d*$");
            if (!reg.IsMatch(buddyNumber))
            {
                AlertForm.ShowAlert(@"查询的账号有误", AlertType.Warning);
                return;
            }
            var selfNumber = GlobalAccountInfo.Account?.AccountNumber;
            var buddy=await _http.GetAsync<BuddyAccountOutput>($"{Api.SearchBuddyInfo}/{selfNumber}/{buddyNumber}");

            if (buddy.Account == null)
            {
                this.panelInfo.Visible = false;
                this.lbTips.Text = @"没有找到改号码的用户";
                return;
            }

            #region 渲染
            this.picAvatar.ImageLocation = ImageHelper.GetServerImage(buddy.Account.Avatar ?? "default.png");
            this.lbAccountNumber.Text = buddy.Account.AccountNumber;
            this.lbArea.Text = buddy.Account.Area;
            this.lbNickName.Text = buddy.Account.NickName;
            this.btnAdd.Visible = !buddy.IsAgree && buddyNumber != selfNumber;
            this.panelInfo.Visible = true;
            #endregion
        }

        private async void btnAdd_Click(object sender, EventArgs e)
        {
            BuddyCreateOrUpdateInput input = new BuddyCreateOrUpdateInput()
            {
                AdderAccountNumber = GlobalAccountInfo.Account.AccountNumber,
                AddeeAccountNumber = lbAccountNumber.Text,
                IsAgree = false
            };
            await _http.PostAsync<bool>(Api.CreateOrUpdateBuddy,input);
            AlertForm.ShowAlert("添加消息已发出，等待对方同意",AlertType.Success,3);
        }

        private void tbSearch_MouseClick(object sender, MouseEventArgs e)
        {
            if (tbSearch.Text.Equals(@"请输入对方的YX号码"))
            {
                tbSearch.Clear();
            }
        }

        private void picSearchClose_Click(object sender, EventArgs e)
        {
            tbSearch.Clear();
        }
        private void picSearchClose_MouseMove(object sender, MouseEventArgs e)
        {
            this.picSearchClose.ImageLocation = ImageHelper.GetStaticImage("closeSearch_active.png");
        }

        private void picSearchClose_MouseLeave(object sender, EventArgs e)
        {
            this.picSearchClose.ImageLocation = ImageHelper.GetStaticImage("closeSearch.png");
        }
    }
}
